<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Validator;

/**
* 商品基本信息控制器
* @method 
*
*/
class GoodsController extends Controller
{
    /**
     * 商品列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)

    {
        //闪存信息
        $request -> flash();
        
        //接收分页 搜索数据
        $search = $request -> input('search', '');
        $count = $request -> input('count', 10);
       
        //查询并返回结果
        return self::goods($search,$count,1);
    }


    /**
     * 商品回收张列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function getRecycle(Request $request)

    {
        //接收分页 搜索数据
        $search = $request -> input('search','');
        $count = $request -> input('count',10);
       
        //查询并返回结果
        return self::goods($search,$count,1);
    }

    


    /**
     * 商品下架.
     *
     * @return \Illuminate\Http\Response
     */
    public function getDown($id)

    {
        //回收商品
        $res = DB::table('goods') -> where('id', $id) -> update(['recycle' => 1]);
   
         // 返回结果
        return self::res($res,'商品下架','/admin/goods/index/' ,'/admin/goods/index/');
    }

    /**
     * 商品上架.
     *
     * @return \Illuminate\Http\Response
     */
    public function getUp($id)

    {
        //回收商品
        $res = DB::table('goods') -> where('id', $id) -> update(['recycle' => 0]);
    
         // 返回结果
        return self::res($res,'商品上架','/admin/goods/recycle/' ,'/admin/goods/recycle/');
    }


    /**
     * 商品添加
     * @数据库不支持事务
     * @return \Illuminate\Http\Response
     */
    public function postInsert(Request $request)
    {
        //接收数据
        $data = $request -> except('_token');
        
        //goods数据
        $goods = $request -> only('title','cl_id','sa_id','br_id','content');
        
        //数据检测

        //插入数据
        $res = DB::table('goods') -> insertGetId($goods);
        
        //返回结果
        return self::res($res,'商品添加','/admin/goods/index/','/admin/goods/add/');

    }


    /**
     * 添加商品页面
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
        //商品类别
        $data = DB::table('goods_class')
                     ->select(DB::raw("concat(path,',',id) as paths,title,id,status"))
                     ->orderBy('paths')
                     ->get();
        foreach ($data as $k => $v) {

            $c = substr_count($data[$k]['paths'], ',');
            
            $str = str_repeat('|---', $c);
            $data[$k]['title'] = $str.$data[$k]['title'];
        }

        //商品销售模式
        $sale = DB::table('goods_sale') -> where('position' ,'!=' ,0) -> get();
        //商品所有品牌
        $brand = DB::table('goods_brand') -> get();
        //显示添加列表
        return view('admin/goods/add',['class' => $data ,'sale' => $sale ,'brand' => $brand]);
    }

    /**
    * 删除商品
    * @是否联合所有关联表进行删除?
    */
    public function getDelete( $id )
    {
        //接收数据并处理
        $res = DB::table('goods') -> where('id',$id) ->delete();
        
        // 返回结果
        return self::res($res,'删除商品','/admin/goods/recycle/','/admin/goods/recycle/');
    }


    /**
    * 商品列表方法
    */
    static private function goods($search,$count,$recycle)
    {
        //接收参数数组
        $var = [];
        $var['search'] = $search;
        $var['count'] = $count;

        //查询数据库
        $data = DB::table('goods')
            ->join('goods_brand', 'goods.br_id', '=', 'goods_brand.id')
            ->join('goods_class', 'goods.cl_id', '=', 'goods_class.id')
            ->join('goods_sale', 'goods.sa_id', '=', 'goods_sale.id')
            ->select('goods.*', 'goods_brand.title as brand', 'goods_class.title as class','goods_sale.title as sale')
            ->where('recycle', '=' ,1)
            ->where('goods.title','like','%'.$search.'%')
            ->where('goods.sa_id','%'.$search.'%')
            ->paginate($count);

        //解析模板
        return view('admin/goods/recycle',['data' => $data,'var' => $var]);;
    }

    
    /**
    * 处理结果函数
    */
     static private function res($res,$message,$url_su,$url_er){
        if ($res) {
         
           //跳转到员工列表并显示成功信息
           return redirect($url_su)
                        ->with(['message'=>$message.'成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect($url_er)
                        ->with(['message'=>$message.'失败','type'=>2]);
        }
    }


    // /**
    //  * 查看商品图片
    //  * @是否让用户一次性修改所有数据
    //  * @return \Illuminate\Http\Response
    //  */
    // public function getEdit(Request $request)
    // {   
    //     //获得版本id
    //     $id = $request -> input('id');
        
    //     //查询所有数据
    //     $data = DB::table('goods')
    //         ->join('goods_brand', 'goods.br_id', '=', 'goods_brand.id')
    //         ->join('goods_class', 'goods.cl_id', '=', 'goods_class.id')
    //         ->join('goods_sale',  'goods.sa_id', '=', 'goods_sale.id')
    //         ->join('goods_version', 'goods.id', '=', 'goods_version.gid')
    //         ->select('goods.*' ,'goods_version.orig as orig', 'goods_version.price as price', 'goods_version.num as num','goods_version.ctime as ctime','goods_version.title as version','goods.*', 'goods_brand.title as brand', 'goods_class.title as class','goods_sale.title as sale')
    //         ->where('goods.id','=',$id)
    //         ->first();
    //     // dd($data);
    //     //商品类别
    //     $class = DB::table('goods_class')
    //                  ->select(DB::raw("concat(path,',',id) as paths,title,id,status"))
    //                  ->orderBy('paths')
    //                  ->get();
    //     foreach ($class as $k => $v) {

    //         $c = substr_count($class[$k]['paths'], ',');
            
    //         $str = str_repeat('|---', $c);
    //         $class[$k]['title'] = $str.$class[$k]['title'];
    //     }
    //     // dd($data);
    //     //商品销售模式
    //     $sale = DB::table('goods_sale') -> where('position' ,'!=' ,0) -> get();
    //     //商品所有品牌
    //     $brand = DB::table('goods_brand') -> get();

    //     //返回模板
    //     return view('admin/goods/edit',['goods' => $data ,'class' => $class ,'sale' => $sale ,'brand' => $brand]);
    // }

    
       // //goods_version数据获得
        // $version = $request -> only('orig','price','num'); 
        // $version['ctime'] = time();
        // $ve_title = $request -> only('ve_title');
        // $version['title'] = $ve_title['ve_title'];
        // $version['gid'] = $gid;
        // $ve_id = DB::table('goods_version') -> insertGetId($version);

        // //version_detail数据获得
        // $de_id = $request -> only('de_id');
        // $detail['pid'] = $de_id['de_id'];
        // $detail['ve_id'] = $ve_id;
        // $de_title = $request -> only('de_title');
        // $detail['title'] = $de_title['de_title'];
        // $de_content = $request -> only('de_content');
        // $detail['content'] = $de_content['de_content'];
        
        // //存入数据
        // $res = DB::table('version_detail') -> insert($detail);
        
        // //验证事物是否提交
        // if ($gid && $ve_id && $res) {
        //     DB::commit();
        //     return redirect('admin/goods/index')
        //         ->with(['message' => '添加成功','type' => 1]);
        // }else{
        //     DB::rollBack();
        //     return back()
        //         ->with(['message' => '添加成功','type' => 2]);
        // }

}
